package am.fp.toolbars
{ 
  import fl.controls.Button;                    
  import flash.events.Event;
  
  import am.fp.Toolbar;
  import am.fp.FPDispatcher;
  import am.fp.Document;
  import am.fp.events.ZoomEvent;
  import am.fp.events.SidePaneEvent;
  
  /**
   * Toolbar containing buttons for changing zoom & view modes (fit page, fit width, show/hide tiles view).
   */
  public class View extends Toolbar
  { 
    protected var m_btnFitPage: Button;
    protected var m_btnFitWidth: Button;
    protected var m_btnTileView: Button;
    protected var m_btnFullscreen: Button;
    protected var m_previousZoom: Number = ZoomEvent.MIN;
    
    /**
     * Constructor. Add buttons and connect listeners.
     */
    public function View()
    {
      m_btnFitPage = addButton("FitPageIcon");
      m_btnFitWidth = addButton("FitWidthIcon");
      m_btnTileView = addButton("TileIcon");
      m_btnFullscreen = addButton("FullscreenIcon");
      
      m_btnFitPage.toggle = true;
      m_btnFitPage.selected = false;
      m_btnFitWidth.toggle = true;
      m_btnFitWidth.selected = false;
      m_btnTileView.toggle = true;
      m_btnTileView.selected = false;
      m_btnFullscreen.toggle = true;
      m_btnFullscreen.selected = false;
      
      m_btnFitPage.addEventListener(Event.CHANGE, onFitPage, false, 0, true);
      m_btnFitWidth.addEventListener(Event.CHANGE, onFitWidth, false, 0, true);
      m_btnTileView.addEventListener(Event.CHANGE, onTileView, false, 0, true);
      m_btnFullscreen.addEventListener(Event.CHANGE, onFullscreen, false, 0, true);
      FPDispatcher.instance.addEventListener(ZoomEvent.CHANGE, onZoomChange, false, 0, true);
      FPDispatcher.instance.addEventListener(ZoomEvent.FULLSCREEN_ESC, onFullscreenEsc, false, 0, true);
      FPDispatcher.instance.addEventListener(SidePaneEvent.CHANGE, onSidePaneChange, false, 0, true);
    }
    
    /**
     * Dispatch event to change zoom mode.
     */
    protected function onFitPage(e: Event): void
    {                            
      if (m_btnFitPage.selected)
      {
        m_previousZoom = Document.instance.currentZoom;
        FPDispatcher.instance.dispatchEvent(new ZoomEvent(ZoomEvent.ZOOM_TO_FIT, ZoomEvent.FIT_PAGE));
        m_btnFitWidth.selected = false;
      }
      else
        FPDispatcher.instance.dispatchEvent(new ZoomEvent(ZoomEvent.CHANGE, m_previousZoom));
    }
    
    protected function onFitWidth(e: Event): void
    {
      if (m_btnFitWidth.selected)
      {
        m_previousZoom = Document.instance.currentZoom;
        FPDispatcher.instance.dispatchEvent(new ZoomEvent(ZoomEvent.ZOOM_TO_FIT, ZoomEvent.FIT_WIDTH));
        m_btnFitPage.selected = false;
      }
      else
        FPDispatcher.instance.dispatchEvent(new ZoomEvent(ZoomEvent.CHANGE, m_previousZoom));
    }

    /**
     * Reset zoom mode buttons when the zoom is modified using the zoom slider.
     */
    protected function onZoomChange(e: ZoomEvent): void
    {
      m_btnFitPage.selected = false;
      m_btnFitWidth.selected = false;
    }

    /**
     * Dispatch event to show/hide the tiles pane.
     */
    protected function onTileView(e: Event): void
    {
      FPDispatcher.instance.dispatchEvent(new SidePaneEvent(SidePaneEvent.TOGGLE, SidePaneEvent.PANE_TILES));
    }
    
    /**
     * Dispatch event to set de/activate fullscreen.
     */
    protected function onFullscreen(e: Event): void
    {
       FPDispatcher.instance.dispatchEvent(new ZoomEvent(m_btnFullscreen.selected ? ZoomEvent.FULLSCREEN_ON : ZoomEvent.FULLSCREEN_OFF));
    }
    
    /**
     * Sync tiles toggle button state.
     */
    protected function onSidePaneChange(e: SidePaneEvent): void
    {
      if (e.paneId == SidePaneEvent.PANE_TILES)
        m_btnTileView.selected = e.visible;
    }
    
    /**
     * Sync fullscreen toggle button state.
     */
    protected function onFullscreenEsc(e: ZoomEvent): void
    {
      m_btnFullscreen.selected = false;
    }
  }
}
